package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.datatables.ShopItemsListConfig;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.ShopItem;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 处理客户端请求购买商城物品
 * @author Frank
 *
 */
public class RequestShopItem extends L2GameClientPacket {
	
	private static final String _C__129_REQUESTSHOPITEM = "[C] 129 RequestShopItem";

	private static final Logger _log = LoggerFactory.getLogger(RequestShopItem.class.getName());
	/** 购买类别 */
	private byte _type;
	/** 物品ID */
	private int _itemID;
	/** 商品uid */
	private long _uid;
	/** 物品数量 */
	private int _count;
	/** 价格(元宝、积分) */
	private int _price;
	
	FuzePcInstance _player;

	ShopItem _item;
	
	@Override
	protected void readImpl() {
		_player = getClient().getActiveChar();
		_uid = readLong();
		_item = ShopItemsListConfig.getInstance().getShopItem(_uid);
		_type = _item.getType();
		_itemID = _item.getId();
		_count = readInt();
	}

	@Override
	protected void runImpl() {
		switch (_type) {
		case ShopItemsListConfig.NEW_ITEM:
		case ShopItemsListConfig.HOT_SELL:
		case ShopItemsListConfig.DISCOUNT_ITEM:
		case ShopItemsListConfig.DAY_ITEM:
		case ShopItemsListConfig.MATERIAL_ITEM:
			buyItemByMoney(_type);
			break;
		case ShopItemsListConfig.POINT_ITEM:
			buyItemByPoint();
			break;
		default:
			break;
		}
	}
	
	/**
	 * 元宝购买
	 * @param type
	 */
	private void buyItemByMoney(byte type) {
		if (_player.getInventory().validateCapacityByItemId(_itemID,  _count)) {
			ShopItem item = ShopItemsListConfig.getInstance().getShopListByType(type).get(_itemID);
			if (item != null) {
				if (type == ShopItemsListConfig.DISCOUNT_ITEM) {
					_price = item.getDiscountPrice();
				} else {
					_price = item.getPrice();
				}
					
				int money = _price * _count;
				
				if (_player.getMoneyYuanBao() >= money) {
//					L2ItemInstance itemInstance = _
					_player.addItem("商城玩家买", _itemID, _count, _player, true);
					_player.reduceMoneyYuanBao("商城玩家买", money, _player, true);
					_player.sendPacket(new SystemMessage(SystemMessageId.TRADE_SUCCESSFUL));  //-- 交易成功
					
//					long playerID = _player.getActingPlayer().getObjectId();
//		    		String palyerName = _player.getActingPlayer().getName();
//		    		String msg = "商城玩家元宝买:" + palyerName + "(" + playerID +  ");" + itemInstance.getName() + "(" + itemInstance.getObjectId() + ")," + _count;
//		    		_log.info(msg, playerID, palyerName);
//		    		msg = "商城玩家元宝买,扣元宝:" + palyerName + "(" + playerID +  ");" + money;
//		    		_log.info(msg, playerID, palyerName);
		    		
					if (item.getPointOfPresent() > 0) {  //-- 赠送积分
						_player.addMoneyPoint("商城玩家买", item.getPointOfPresent() * _count, _player, true);
//						msg = "商城玩家元宝买,得积分:" + palyerName + "(" + playerID +  ");" + (item.getPointOfPresent() * _count);
//			    		_log.info(msg, playerID, palyerName);
					}
					
//					msg = "商城玩家元宝买,得物品:" + itemInstance.getName() + "(" + itemInstance.getObjectId() + ");" + _count;
//					_log.info(msg, itemInstance.getObjectId(), itemInstance.getName());
					
				} else {
					_player.sendPacket(new SystemMessage(SystemMessageId.NO_ENOUGH_YUANBAO)); //-- 元宝不够
				}
			}
		} else {
			_player.sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));  //-- 背包满
		}
	}
	
	/**
	 * 积分购买
	 */
	private void buyItemByPoint() {
        if (_player.getInventory().validateCapacityByItemId(_itemID, _count)) {
        	ShopItem item = ShopItemsListConfig.getInstance().getShopListByType(ShopItemsListConfig.POINT_ITEM).get(_itemID);
			if (item != null) {
				_price = item.getPriceOfPoint();
				int money = _price * _count;
				
				if (_player.getMoneyPoint() >= money) {
//					L2ItemInstance itemInstance = 
					_player.addItem("商城玩家买", _itemID, _count, _player, true);
					_player.reduceMoneyPoint("商城玩家买", money, _player, true);
					_player.sendPacket(new SystemMessage(SystemMessageId.TRADE_SUCCESSFUL));  //-- 交易成功
					
//					long playerID = _player.getActingPlayer().getObjectId();
//		    		String palyerName = _player.getActingPlayer().getName();
//		    		String msg = "商城玩家积分买:" + palyerName + "(" + playerID +  ");" + itemInstance.getName() + "(" + itemInstance.getObjectId() + ")," + _count;
//		    		_log.info(msg, playerID, palyerName);
//		    		msg = "商城玩家积分买,扣积分:" + palyerName + "(" + playerID +  ");" + money;
//		    		_log.info(msg, playerID, palyerName);
					
					if (item.getPointOfPresent() > 0) {  //-- 赠送积分
						_player.addMoneyPoint("商城玩家买", item.getPointOfPresent() * _count, _player, true);
//						msg = "商城玩家积分买,得积分:" + palyerName + "(" + playerID +  ");" + (item.getPointOfPresent() * _count);
//			    		_log.info(msg, playerID, palyerName);
					}
					
//					msg = "商城玩家积分买,得物品:" + itemInstance.getName() + "(" + itemInstance.getObjectId() + ");" + _count;
//					_log.info(msg, itemInstance.getObjectId(), itemInstance.getName());
				} else {
					_player.sendPacket(new SystemMessage(SystemMessageId.NO_ENOUGH_POINT)); //-- 积分不够
				}
			}
		} else {
			_player.sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));  //-- 背包满
		}
	}
	
	@Override
	public String getType() {
		return _C__129_REQUESTSHOPITEM;
	}
}
